package com.itheima.refect;

import com.itheima.domain.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/*
获取构造方法
带s 的获取多个
不带s 获取单个
带declared 获取所有
不带daclared 只能获取public


 */
public class Demo01 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Student> clazz = (Class<Student>) Class.forName("com.itheima.domain.Student");
//        Student student = clazz.newInstance();
//        System.out.println(student);
        Constructor<Student> declaredConstructor3 = clazz.getDeclaredConstructor();
        declaredConstructor3.setAccessible(true);
        Student student = declaredConstructor3.newInstance();
        System.out.println(student);
        Constructor<?>[] constructors = clazz.getConstructors();
        System.out.println("获取所有公有的");
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
        Constructor<?> constructor = clazz.getConstructor();
        System.out.println("获取单个共有的");
        System.out.println("空参数的公有的:"+constructor);
        Constructor<?> constructor1 = clazz.getConstructor(String.class, int.class, String.class);
        System.out.println("全参数的公有的:"+constructor1);
//        Constructor<?> constructor2 = clazz.getConstructor(String.class);
//        System.out.println("String的公有的:"+constructor2);
        Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
        System.out.println("获取所有 包括私有");
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
          Constructor<?> declaredConstructor = clazz.getDeclaredConstructor(String.class);
        System.out.println("String类型私有"+declaredConstructor);
        //暴力反射
        declaredConstructor.setAccessible(true);
        Student zhangsan = (Student) declaredConstructor.newInstance("张三");
        System.out.println(zhangsan);
        Constructor<?> declaredConstructor1 = clazz.getDeclaredConstructor(int.class);
        System.out.println("int类型私有"+declaredConstructor1);
        Constructor<?> declaredConstructor2 = clazz.getDeclaredConstructor();
        System.out.println("无参数公有"+declaredConstructor2);


    }



}
